package com.hs.controller;
/*
 * @Author:iWeeper
 * @Date:2022/6/23 16:33
 * @Version:1.0
 * */

import com.hs.entity.Grade;
import com.hs.entity.TbUser;
import com.hs.service.StudentService;
import com.hs.utils.ResultUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.enterprise.context.ApplicationScoped;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /*跳转到分数列表*/
    @RequestMapping("/score")
    public String stuScore(){
        return "student/score-list";
    }

    /*获取数据列表*/
    @RequestMapping("scoreList/data")
    @ResponseBody
    public Map scoreListData(){
        /*对身份信息进行判断*/
        Subject subject= SecurityUtils.getSubject();
        TbUser user= (TbUser) subject.getPrincipal();
        Session shiroSession= subject.getSession();
        TbUser tbUser = (TbUser) shiroSession.getAttribute("user");
        /*记录当前的登录人Id为CurrentId*/
        String CurrentId= String.valueOf(tbUser.getId());
        List<Grade> list=studentService.findGradeListBySid(CurrentId);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    @RequestMapping("/scoreLineJson")
    protected void scoreLineJson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        /*防止乱码*/
        Subject subject= SecurityUtils.getSubject();
        TbUser user= (TbUser) subject.getPrincipal();
        Session shiroSession= subject.getSession();
        TbUser tbUser = (TbUser) shiroSession.getAttribute("user");
        //调用查询方法，返回图表的数据
        List<Map<String,Object>> list = studentService.queryEchartsByStudentId(tbUser.getId());
        ResultUtils.success(response,list,"ok");
    }

    @RequestMapping("/view")
    public String getCouTable(Model model, HttpServletRequest request, HttpServletResponse response){
        Subject subject= SecurityUtils.getSubject();
        TbUser user= (TbUser) subject.getPrincipal();
        Session shiroSession= subject.getSession();
        TbUser tbUser = (TbUser) shiroSession.getAttribute("user");
        return "manager/couTable";
    }
}
